package com.whfc.ms.api.safety.controller;

import com.whfc.common.base.BaseController;
import com.whfc.common.result.ListData;
import com.whfc.common.result.PageData;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.SessionAttr;
import com.whfc.fuum.entity.SysUser;
import com.whfc.ms.api.safety.service.MsSafetyDangerService;
import com.whfc.safety.dto.*;
import com.whfc.safety.param.SafetyDangerAddParam;
import com.whfc.safety.param.SafetyDangerAttachSaveParam;
import com.whfc.safety.param.SafetyDangerEditParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Description 危大工程
 * @Author hw
 * @Date 2021-08-19 15:54
 * @Version 1.0
 */
@RestController
@RequestMapping("/ms/api/safety/danger")
public class SafetyDangerController extends BaseController {

    @Autowired
    private MsSafetyDangerService msSafetyDangerService;

    @GetMapping("dict/list")
    public Result dictList(@SessionAttribute(SessionAttr.MS_USER) SysUser user) {
        logger.info("危大字典列表,loginer:{}", user.getUsername());
        ListData<SafetyDangerDictDTO> list = msSafetyDangerService.dictList();
        return ResultUtil.success(list);
    }

    @GetMapping("dict/control/list")
    public Result dictControlList(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                  @RequestParam("dictId") Integer dictId) {
        logger.info("危大字典-管控要点列表,loginer:{},dictId:{}", user.getUsername(), dictId);
        ListData<SafetyDangerDictControlDTO> list = msSafetyDangerService.dictControlList(dictId);
        return ResultUtil.success(list);
    }

    @GetMapping("statistical")
    public Result statistical(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                              @RequestParam("deptId") Integer deptId) {
        logger.info("统计危大工程台账,loginer:{},deptId:{}", user.getUsername(), deptId);
        DangerStatisticalDTO data = msSafetyDangerService.statistical(deptId);
        return ResultUtil.success(data);
    }

    @GetMapping("list")
    public Result list(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                       @RequestParam("deptId") Integer deptId,
                       @RequestParam("pageNum") Integer pageNum,
                       @RequestParam("pageSize") Integer pageSize) {
        logger.info("危大工程台账列表,loginer:{},deptId:{}，pageNum：{},pageSize：{}", user.getUsername(), deptId, pageNum, pageSize);
        PageData<SafetyDangerDTO> list = msSafetyDangerService.list(deptId, pageNum, pageSize);
        return ResultUtil.success(list);
    }

    @GetMapping("export")
    public Result exportDanger(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                               @RequestParam("deptId") Integer deptId) {
        logger.info("危大工程台账导出,loginer:{},deptId:{}", user.getUsername(), deptId);
        msSafetyDangerService.exportDanger(deptId);
        return ResultUtil.success();
    }

    @PostMapping("add")
    public Result addDanger(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                            @Validated @RequestBody SafetyDangerAddParam param) {
        logger.info("新建危大工程台账,loginer:{},param：{}", user.getUsername(), param);
        msSafetyDangerService.addDanger(param);
        return ResultUtil.success();
    }

    @PostMapping("edit")
    public Result editDanger(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                             @Validated @RequestBody SafetyDangerEditParam param) {
        logger.info("编辑危大工程台账,loginer:{},param：{}", user.getUsername(), param);
        msSafetyDangerService.editDanger(param);
        return ResultUtil.success();
    }

    @PostMapping("del/{dangerId}")
    public Result delDanger(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                            @PathVariable("dangerId") Integer dangerId) {
        logger.info("删除危大工程台账,loginer:{},dangerId：{}", user.getUsername(), dangerId);
        msSafetyDangerService.delDanger(dangerId);
        return ResultUtil.success();
    }

    @GetMapping("ext")
    public Result dangerExt(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                            @RequestParam("dangerId") Integer dangerId,
                            @RequestParam("type") Integer type,
                            @RequestParam("pageNum") Integer pageNum,
                            @RequestParam("pageSize") Integer pageSize) {
        logger.info("查询当前进度、施工旁站、一般验收、排查记录,loginer:{},dangerId:{}，type：{}，pageNum：{},pageSize：{}",
                user.getUsername(), dangerId, type, pageNum, pageSize);
        PageData<SafetyDangerExtDTO> list = msSafetyDangerService.dangerExt(dangerId, type, pageNum, pageSize);
        return ResultUtil.success(list);
    }

    @PostMapping("ext/del/{extId}")
    public Result dangerExtDel(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                               @RequestParam("type") Integer type,
                               @RequestParam("dangerId") Integer dangerId,
                               @PathVariable("extId") Integer extId) {
        logger.info("删除当前进度、施工旁站、一般验收、排查记录,loginer:{},dangerId:{},type:{},extId：{}", user.getUsername(), dangerId, type, extId);
        msSafetyDangerService.dangerExtDel(dangerId, extId, type);
        return ResultUtil.success();
    }

    @GetMapping("attach")
    public Result dangerAttachList(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                   @RequestParam("dangerId") Integer dangerId) {
        logger.info("危大工程资料列表,loginer:{},dangerId:{}", user.getUsername(), dangerId);
        ListData<SafetyDangerAcceptDTO> list = msSafetyDangerService.dangerAttachList(dangerId);
        return ResultUtil.success(list);
    }

    @PostMapping("attach/{attachId}")
    public Result dangerAttachDel(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                  @PathVariable("attachId") Integer attachId) {
        logger.info("删除危大资料,loginer:{},attachId：{}", user.getUsername(), attachId);
        msSafetyDangerService.dangerAcceptDel(attachId);
        return ResultUtil.success();
    }

    @PostMapping("attach/save")
    public Result dangerAttachSave(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                   @Validated @RequestBody SafetyDangerAttachSaveParam param) {
        logger.info("保存危大文件,loginer:{},param：{}", user.getUsername(), param);
        msSafetyDangerService.dangerAttachSave(param);
        return ResultUtil.success();
    }

    @GetMapping("check/list")
    public Result dangerCheckList(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                  @RequestParam("dangerId") Integer dangerId,
                                  @RequestParam("pageNum") Integer pageNum,
                                  @RequestParam("pageSize") Integer pageSize) {
        logger.info("问题隐患列表,loginer:{},dangerId:{}，pageNum：{},pageSize：{}",
                user.getUsername(), dangerId, pageNum, pageSize);
        PageData<SafetyCheckDTO> list = msSafetyDangerService.dangerCheckList(dangerId, pageNum, pageSize);
        return ResultUtil.success(list);
    }

    @GetMapping("check/details")
    public Result dangerCheckDesc(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                  @RequestParam("checkId") Integer checkId) {
        logger.info("问题隐患列表,loginer:{},checkId：{}", user.getUsername(), checkId);
        SafetyCheckDescDTO data = msSafetyDangerService.dangerCheckDesc(checkId);
        return ResultUtil.success(data);
    }

    @PostMapping("check/del/{checkId}")
    public Result dangerCheckDel(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                 @RequestParam("dangerId") Integer dangerId,
                                 @PathVariable("checkId") Integer checkId) {
        logger.info("删除问题隐患,loginer:{},dangerId:{},checkId：{}", user.getUsername(), dangerId, checkId);
        msSafetyDangerService.dangerCheckDel(dangerId, checkId);
        return ResultUtil.success();
    }

    @GetMapping("accept/list")
    public Result dangerAcceptList(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                   @RequestParam("dangerId") Integer dangerId,
                                   @RequestParam("pageNum") Integer pageNum,
                                   @RequestParam("pageSize") Integer pageSize) {
        logger.info("验收表验收列表,loginer:{},dangerId:{}，pageNum：{},pageSize：{}",
                user.getUsername(), dangerId, pageNum, pageSize);
        PageData<SafetyDangerAcceptDTO> list = msSafetyDangerService.dangerAcceptList(dangerId, pageNum, pageSize);
        return ResultUtil.success(list);
    }


    @PostMapping("accept/{attachId}")
    public Result dangerAcceptDel(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                  @PathVariable("attachId") Integer attachId) {
        logger.info("删除危大资料,loginer:{},attachId：{}", user.getUsername(), attachId);
        msSafetyDangerService.dangerAcceptDel(attachId);
        return ResultUtil.success();
    }

    @PostMapping("accept/uploadImg")
    public Result acceptUploadImg(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                  MultipartFile file,
                                  @RequestParam("dangerId") Integer dangerId,
                                  @RequestParam("name") String name,
                                  @RequestParam("size") Integer size) {
        logger.info("验收表验收文件上传,loginer:{}，dangerId：{}，name：{}，size:{}", user.getUsername(), dangerId, name, size);
        SafetyDangerAttachSaveParam param = new SafetyDangerAttachSaveParam();
        param.setUserId(user.getId());
        param.setUserName(user.getNickname());
        param.setDangerId(dangerId);
        msSafetyDangerService.acceptUploadImg(file, param, name, size);
        return ResultUtil.success();
    }

    @PostMapping("commit/{dangerId}")
    public Result dangerComplete(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                 @PathVariable("dangerId") Integer dangerId) {
        logger.info("完成危大隐患问题,loginer:{},dangerId:{}", user.getUsername(), dangerId);
        msSafetyDangerService.dangerComplete(dangerId);
        return ResultUtil.success();
    }

    @GetMapping("control/list")
    public Result dangerControlList(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                    @RequestParam("dangerId") Integer dangerId) {
        logger.info("管控要点,loginer:{},dangerId:{}", user.getUsername(), dangerId);
        ListData<SafetyDangerControlDTO> list = msSafetyDangerService.dangerControlList(dangerId);
        return ResultUtil.success(list);
    }


}
